package mail.verify.slider;

import mail.util.Chaos;

import java.util.ArrayList;
import java.util.List;

import static com.han.http.util.Print.printlnInfo;


/**
 * @author: Hanjiafeng
 * @date: Created in 10:29 2019/2/2
 * @desc: 滑块轨迹生成器, 先加速, 然后匀速, 最后减速
 */
public class TrackMaker {

    public static void main(String... args) {
        t(170);
    }

    public static List<MoveEntity> generateTrack(int distance) {

        printlnInfo(distance);

        List<MoveEntity> firstStage = normalStage((double) distance * (double) 2 / (double) 170, 17);
        List<MoveEntity> secondStage = normalStage((double) distance * (double) 12 / (double) 170, 50);
        List<MoveEntity> thirdStage = normalStage((double) distance * (double) 52 / (double) 170, 100);
        List<MoveEntity> forthStage = normalStage((double) distance * (double) 75 / (double) 170, 186);
        List<MoveEntity> fifthStage = normalStage((double) distance * (double) 17 / (double) 170, 113);
        List<MoveEntity> sixthStage = normalStage((double) distance * (double) 5 / (double) 170, 80);

        List<MoveEntity> trackList = new ArrayList<>(160);
        trackList.addAll(firstStage);
        trackList.addAll(secondStage);
        trackList.addAll(thirdStage);
        trackList.addAll(forthStage);
        trackList.addAll(fifthStage);
        trackList.addAll(sixthStage);


        printlnInfo(count(firstStage));
        printlnInfo(count(secondStage));
        printlnInfo(count(thirdStage));
        printlnInfo(count(forthStage));
        printlnInfo(count(fifthStage));
        printlnInfo(count((sixthStage)));

        int t = 0;
        int x = 55;
        for (MoveEntity entity : trackList) {
            t += entity.getSleepTime();
            x += entity.getX();
            System.out.println(t + "," + x);
        }

        return trackList;
    }

    private static int count(List<MoveEntity> moveEntities) {
        int c = 0;
        for (MoveEntity entity : moveEntities) {
            if (entity.getX() != 0) {
                c++;
            }
        }
        return c;
    }

    private static List<MoveEntity> normalStage(double point, int moveTime) {
        List<MoveEntity> moveEntityList = new ArrayList<>();
        double interval;
        double m;
        for (int i = 1; i <= moveTime; i++) {
            MoveEntity entity;
            interval = (double) moveTime / point;
            m = (double) i % interval;
            if (m <= 1) {
                entity = new MoveEntity(1, 0, 0);
            } else {
                entity = new MoveEntity(0, 0, 0);
            }
            moveEntityList.add(entity);
        }

        return moveEntityList;
    }

    public static List<MoveEntity> t(int distance) {
        double offset = 0;
        double turnDis = distance * 3.0 / 5.0;
        double t = 0.2;
        double v = 0;
        double a;
        double moveDis;
        int sleepTime;
        List<MoveEntity> moveEntityList = new ArrayList<>();
        while (offset < distance) {
            if (offset < turnDis) {
                a = 2;
            } else {
                a = -3.0;
            }
            moveDis = v * t + 0.5 * a * t * t;
            moveDis = Math.round(moveDis);
            offset += moveDis;
            v = v + a * t;
            sleepTime = (int) Chaos.closeByInt(17, 5);
            MoveEntity entity = new MoveEntity((int) moveDis, 0, sleepTime);
            moveEntityList.add(entity);
        }
        printlnInfo(moveEntityList.get(moveEntityList.size() - 1));

        return moveEntityList;
    }
}
